
# 综合 xpath 和 selenium 的尝试

import requests
from lxml import etree
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.by import By

def req(url, verifyvalue=True, dire=True):
    headersvalue = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36 Edg/94.0.992.38',
                    }
    r = requests.get(url, headers=headersvalue, timeout=5, verify=verifyvalue)
    html = etree.HTML(r.text)
    if dire:
        return html
    else:
        return r.status_code

def get_pages():
    # 构造一个名为 pages 的 tuple，里面各个元素为(page_name, page_url)
    html = req('https://shouku123.com/tiantian')
    page_element = html.xpath('//div[@class="container url-group"]/ol')[0]
    page_names = page_element.xpath('li[1]/text()') + page_element.xpath('li/a/text()')
    # 或者这样：page_names = html.xpath('string(//ol[@class="my-breadcrumb clearfix my-app-nav"])').split()
    page_urls = ['https://shouku123.com/tiantian'] + page_element.xpath('li/a/@href')
    pages = tuple(zip(page_names, page_urls))
    return pages

def get_url(pages):
    # 构造一个名为 groups_dict 的 dict，键为每个分类的名字，值为一个列表，列表的每个元素是由网站名及其链接组成的一个二元元组
    # {'热站榜': [(name, url), (name, url),,, ], }
    point = '1. 默认    2. 海外剧    3. 素材_资料    4. 动漫_动画    5. 软件    6. 视频解析    7. 影视下载    8. 其他'
    print('{}\n{}\n{}'.format('-'*140, point, '-'*140))
    n = int(input('需要检查哪个？\n'))
    global groups_dict
    groups_dict = {}
    page = pages[n-1]
    html = req(page[1])
    node_ul = html.xpath('//ul[@class="list-group collapse in urls "]')
    for ul in node_ul:
        panel_head = ul.xpath('./@title')[0]
        items = []
        onclicks = ul.xpath('.//span[@title="点击查看二维码"]/@onclick')   # 路径不加 li，onclicks 等于整个网页内符合条件的节点，加上li，那就只是ul下的，为什么？
        for onclick in onclicks:
            qrContent = onclick.split('Show(')[1].split(',')
            item = eval(qrContent[0]), eval(qrContent[1])
            items.append(item)
        groups_dict[panel_head] = items
    return page

def try_except(item, results):
    try:
        code = req(item[1], dire=False)
    except requests.Timeout:
        result = f'\n{item[0]} | Timeout | {item[1]}\n'
        print(result)
        results.append(result.strip())
    except requests.exceptions.SSLError:
        try:
            code = req(item[1], verifyvalue=False, dire=False)
        except requests.exceptions.SSLError:
            result = f'\n{item[0]} | SSLError | {item[1]}\n'
            print(result)
            results.append(result.strip())
        except:
            result = f'\n{item[0]} | 发生未知错误 1 | {item[1]}\n'
            print(result)
            results.append(result.strip())
        else:
            if code == 200:
                print(f'{item[0]} | 可正常访问')
            else:
                result = '\n{} | 响应状态码：{} | {}\n'.format(item[0], code, item[1])
                print(result)
                results.append(result.strip())
    except requests.ConnectionError:
        result = f'\n{item[0]} | ConnectionError | {item[1]}\n'
        results.append(result.strip())
        print(result)
    except requests.exceptions.MissingSchema:
        item = item[0], 'https://' + item[1]
        try_except(item, results)
    except:
        result = f'\n{item[0]} | 发生未知错误 2 | {item[1]}\n'
        print(result)
        results.append(result.strip())
    else:
        if code == 200:
            print(f'{item[0]} | 可正常访问')
        else:
            result = '\n{} | 响应状态码：{} | {}\n'.format(item[0], code, item[1])
            print(result)
            results.append(result.strip())

def get_status(page):
    global groups_dict
    print('\n“{}” 主题下共有 {} 个分类，分别是：'.format(page[0], len(groups_dict)))
    print('-'*140)
    for group in groups_dict:
        n = len(groups_dict[group])
        print(f'{group}({n})',end='   ')
    print()
    print('{}\n0. 一次性测试全部    1.单独测试分类，做出选择：'.format('-'*140))
    choice = int(input(''))
    if choice:
        c = input('输入需要测试分类的名称：')
        temp = {}
        temp[c] = groups_dict[c]
        groups_dict = temp
    results = []
    n = 1
    for key, value in groups_dict.items():
        print('\n现在开始检查分类 “{}” （{}/{}）：\n'.format(key, n, len(groups_dict)))
        n += 1
        for item in value:
            try_except(item, results)
    if results:
        print('\n第一遍测试结束！结果汇总如下：\n')
        for result in results:
            print(result)
        if len(results) > 9:
            with open('d:/低调观影测试结果.txt', 'a', encoding='utf-8') as f:
                f.write('\n'.join(results))
    else:
        print('\n测试结束，网站全部可以正常访问！')
    return results


def confirm(results):
    browser = webdriver.Chrome()
    browser.maximize_window()
    for result in results:
        name, url = result.split('|')[0], result.split('|')[-1]
        browser.get('https://gualemang.com/')
        browser.find_element(By.XPATH, '//div[@class="input-group"]/input').send_keys(url)
        browser.find_element(By.XPATH, '//div[@class="input-group-append"]/button').click()
        browser.find_element(By.XPATH, '//div[@class="input-group"]/input').send_keys(name)
        next = input('下一个：')
        if next == '\r':
            print('现在打开的是：{}，网址：{}'.format(name, url))
            continue
        else:
            print('控制台输入错误！！！')
            print('现在打开的是：{}，网址：{}'.format(name, url))
            continue

def main():
    pages = get_pages()
    page = get_url(pages)
    results = get_status(page)
    if not results:
        confirm(results)
    

if __name__ == '__main__':
    main()


